<template>
  <j-modal :title="title" :width="800" :visible="visible" :confirmLoading="confirmLoading" switchFullscreen
    @ok="handleOk" @cancel="handleCancel" cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="名称">
          <a-input placeholder="请输入名称" v-decorator="['title', { rules: [ { required: true, message: '请输入名称!' },]}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客服">
          <a-input placeholder="请输入客服" v-decorator="['name', { rules: [ { required: true, message: '请输入客服!' },]}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="电话">
          <a-input placeholder="请输入电话" v-decorator="['phone', { rules: [ { required: true, message: '请输入电话!' },]}]" />
        </a-form-item>
        <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-input placeholder="请输入备注" v-decorator="['remark', {}]" />
        </a-form-item> -->
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="门店照片">
          <j-image-upload text="上传" v-decorator="['logo', {}]"></j-image-upload>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="地址">
          <a-input placeholder="请输入地址" v-decorator="['address', { rules: [ { required: true, message: '请输入地址!' },]}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="经纬度">
          <AddressSelect :value="point" @change="initAdd"></AddressSelect>
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="膜类销售额">
          <a-input placeholder="请输入数据" v-decorator="['workstationsNumber', { rules: [ { required: true, message: '请输入销售额!' },]}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="工位数量">
          <a-input placeholder="请输入数据" v-decorator="['salesNumber', {}]" />
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from "moment"
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
import JUpload from '@/components/jeecg/JUpload'
import JImageUpload from '@/components/jeecg/JImageUpload'
import AddressSelect from '../comp/AddressSelect'

export default {
  name: "ShopModal",
  components: { JUpload, JImageUpload, AddressSelect },
  data() {
    return {
      title: "操作",
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        createUser: { rules: [{ required: true, message: '请输入更新人!' }] },
        updateUser: { rules: [{ required: true, message: '请输入更新人!' }] },
      },
      url: {
        add: "/mpMini/shop/add",
        edit: "/mpMini/shop/edit",
      },
      point: [],
    }
  },
  created() {
  },
  methods: {
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      if(record.logo===null||record.logo===''){record.logo="https://www.manpingou.com/uploads/allimg/220923/25-220923145Q3491.jpg"}
      this.model = Object.assign({}, record);
      if (this.model.longitude) {
        this.point[0] = this.model.longitude;
        this.point[1] = this.model.latitude;
      } else {
        this.point = [];
      }
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'logo', 'title', 'name', 'phone', 'address', 'workstationsNumber', 'salesNumber'))
        //时间格式化
      });

    },
    close() {
      this.$emit('close');
      this.visible = false;
    },
    isValidNumber(num) {
      // 使用正则表达式来检查字符串是否符合非负整数的规则  
      const regex = /^(0|[1-9][0-9]*)$/;
      return regex.test(num); // 将num转换为字符串后进行匹配  
    },
    handleOk() {
      const that = this;
      this.form.validateFields((err, values) => {
        console.log("___");
        console.log(values);
        console.log(err);
        if(err) return;
        if (!this.isValidNumber(values.salesNumber) || !this.isValidNumber(values.workstationsNumber)) {
          that.$message.warning("膜类销售额和工位数量必须为数字且非负");
        } else {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values ,{'audit':"1"});
          //时间格式化
          console.log("_____")
          console.log(httpurl)
          console.log(formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        }
      })

    },

    handleCancel() {
      this.close()
    },
    initAdd(val) {
      this.point = val.location;
      this.model.longitude = val.location[0];
      this.model.latitude = val.location[1];
    }

  }
}
</script>

<style lang="less" scoped></style>